package com.example.demo.service;

import com.example.demo.domain.Girl;
import com.example.demo.enums.ResultEnums;
import com.example.demo.exception.GirlException;
import com.example.demo.respository.GirlRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by wangbing on 2018/3/28.
 */
@Service
public class GirlService {
    @Autowired
    private GirlRespository girlRespository;

    /**
     * 对方法加事务
     */
    @Transactional
    public void insertTwo(){
        Girl girlA = new Girl();
        girlA.setCupSize("c");
        girlA.setAge(23);
        girlRespository.save(girlA);

        Girl girlB = new Girl();
        girlB.setCupSize("c");
        girlB.setAge(23);
        girlRespository.save(girlB);
    }

    public void getAge(Integer id) throws  Exception{
        Girl girl = girlRespository.findOne(id);
        int age = girl.getAge();
        if (age<10){
            throw new GirlException(ResultEnums.PRIMARY_SCHOOL);

        }else if(age<16&&age>=10){
            throw new GirlException(ResultEnums.MINDDLE_SCHOOL);

        }
    }

    public Girl findOne(Integer id){
        return girlRespository.findOne(id);
    }
}
